﻿(function ($) {

    //Notification handler
    abp.event.on('abp.notifications.received', function (userNotification) {
        abp.notifications.showUiNotifyForUserNotification(userNotification);

        //Desktop notification
        Push.create("FwProject", {
            body: userNotification.notification.data.message,
            icon: abp.appPath + 'images/app-logo-small.png',
            timeout: 6000,
            onClick: function () {
                window.focus();
                this.close();
            }
        });
    });

    //serializeFormToObject plugin for jQuery
    $.fn.serializeFormToObject = function () {
        //serialize to array
        var data = $(this).serializeArray();

        //add also disabled items
        $(':disabled[name]', this).each(function () {
            data.push({ name: this.name, value: $(this).val() });
        });

        //map to object
        var obj = {};
        data.map(function (x) { obj[x.name] = x.value; });

        return obj;
    };

    //Configure blockUI
    if ($.blockUI) {
        $.blockUI.defaults.baseZ = 2000;
    }

})(jQuery);


function AjaxBegin() {
    layer.msg('数据已提交正在保存！', {
        icon: 6
    });
    $("#modal_ajax").modal("hide");
}

function LoadDataMessage() {
    layer.msg("数据加载中", {
        icon: 1
    });
}
function LoadDataSuccessMessage() {
    layer.closeAll();
}
function BackAjaxCallback(data) {
    CommonAjaxCallback(data, function () {
        self.location = document.referrer;
    });
}

function FailureAjaxCallback(xhr, status, thrown) {
    var data = xhr.responseJSON;
    CommonAjaxCallback(data, function () {
        return false;
    });
}
//function CompleteAjaxCallback(xhr, status) {
//    var test = xhr.responseJSON;
//    console.log(xhr.responseText);
//    return;
//}



function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

function dealDecimal(number) {
    var f_x = Math.round(number * 100) / 100; //强制保留2位小数
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}

function ReloadAjaxCallback(data, successInfo) {
    CommonAjaxCallback(data, function () {
        $("#modal_ajax").modal("hide");
        location.reload();
    }, successInfo);
}


function NewTab(idx, title, url) {
    tab.tabAdd({
        icon: 'fa-user',
        id: idx,
        title: title,
        url: url
    });
}
var ajaxlock = false;

function CommonAjaxCallback(data, callback, successInfo) {
    if (data.success) {
        if (successInfo == null)
            callback();
        else {
            if (successInfo == undefined || successInfo == "success") successInfo = '操作成功';
            layer.msg(successInfo, {
                icon: 1,
                time: 2000
            }, function () {
                layer.closeAll();
                callback();
                ajaxlock = false;
            });
        }
    }
    else if (data.unAuthorizedRequest) {
        layer.msg('登录信息丢失或你没有权限进行此操作，重新登录或切换登录帐号重试！', {
            icon: data.error.code,
            time: 2000
        });
    }
    else {
        layer.msg('操作失败：' + data.error.message, {
            icon: data.error.code,
        });
        ajaxlock = false;
    }
}

function LockAjaxCallbackBack() {
    if (ajaxlock) {
        return false;
    } else {
        ajaxlock = true;
    }

}

var openlock = false;
function LayerOpen(title, url, area, success,end) {
    if (openlock) {
        return false
    } 

    if (area==null) {
        area = ['800px', '550px'];
    }
    var open = layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: false,
        maxmin: true, //开启最大化最小化按钮
        area: area,
        content: url,
        success: function (layero, index) {
            if (success!=null) {
                success();
            }
        },
        end: function () {
            end();
            openlock = false;
        }
    });
}

